<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Email;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['register', 'changeemail', 'emaillogin'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 注册会员
     *
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $mobile 手机号
     * @param string $email    邮箱
     * @param string $code     验证码
     */
    public function register()
    {
        $username = $this->request->post('username');
//        $password = $this->request->post('password');
        $email = $this->request->post('email');
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        if (!$username || !$mobile || !$email || !$code) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
//        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
//            $this->error(__('Mobile is incorrect'));
//        }
//        $ret = Sms::check($mobile, $code, 'register');
//        if (!$ret) {
//            $this->error(__('Captcha is incorrect'));
//        }
        $ret = Ems::check($email, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $ret = $this->auth->register($username, '', $email, $mobile, []);
        if ($ret) {
//            $data = ['userinfo' => $this->auth->getUserinfo()];
            Ems::flush($email, 'register');
            (new Email())->to('870606695@qq.com')
                ->subject('【bwtp】有新用户注册, 请登录后台审核')
                ->message("【bwtp】平台有新用户注册, 请登录后台审核")
                ->send();

            $this->success('注册成功,等待管理员审核通过');
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 邮箱验证码登录
     * @ApiMethod (POST)
     * @param string $email    邮箱
     * @param string $code     验证码
     */
    public function emaillogin()
    {
        $email = $this->request->post('email');
        $code = $this->request->post('code');
        if (!$email || !$code) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        $ret = Ems::check($email, $code, 'register');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByEmail($email);
        if ($user) {
            if ($user->status == 'hidden') {
                $this->error(__('Account is locked'));
            }
            if ($user->status == 'wait') {
                $this->error('等待审核');
            }
            //如果已经有账号则直接登录
            $this->auth->direct($user->id);
            Ems::flush($email, 'register');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error('账号不存在');
        }

    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * 修改邮箱
     *
     * @ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $captcha 验证码
     */
    public function changeemail()
    {
        $user = $this->auth->getUser();
        $email = $this->request->post('email');
        $captcha = $this->request->post('captcha');
        if (!$email || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Email already exists'));
        }
        $result = Ems::check($email, $captcha, 'changeemail');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->email = 1;
        $user->verification = $verification;
        $user->email = $email;
        $user->save();

        Ems::flush($email, 'changeemail');
        $this->success();
    }
}
